package zhieasy.cn.xpu_helper.controller.initiation;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhieasy.cn.common.utils.ResultVOUtil;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.common.SiteLocation;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.model.initiation.SchoolSiteType;
import zhieasy.cn.service.initiation.AdminCollegeService;
import zhieasy.cn.service.initiation.SiteLocationService;

import java.util.List;

/**
 * @author sofency
 * @date 2020/8/2 7:18
 * @package IntelliJ IDEA
 * @description
 */
@RestController
@RequestMapping("/initiation")
@Api(tags = "开学地图与,地点相关的api")
public class SiteLocationController {

    @Autowired
    SiteLocationService siteLocationService;

    @Autowired
    AdminCollegeService adminCollegeService;

    /**
     * 添加地点
     * @param siteLocation
     * @return
     */
    @ApiOperation(value = "添加地点",notes = "添加地点")
    @RequestMapping(value = "/place",method = RequestMethod.POST)
    public ResultVO addPlace(@ApiParam("siteLocation对象") @RequestBody SiteLocation siteLocation){
        boolean b = siteLocationService.insertPlace(siteLocation);
        if (b){
            return ResultUtils.success();
        }
        return ResultUtils.error();
    }



    /**
     * 更新地点
     * @param siteLocation
     * @return
     */
    @ApiOperation(value = "更新地点",notes = "更新地点")
    @RequestMapping(value = "/place",method = RequestMethod.PUT)
    public ResultVO updatePlace(@RequestBody SiteLocation siteLocation){
        String openId = WechatUser.getWechatUserOpenIdFromSecurityContext();
        //首先根据创建者的unionId找到学员号
        System.out.println(siteLocation);
        String creatorUser = siteLocation.getCreatorUser();
        //判断登录者的学院号时候和创建者的学院号一样
        if(!"".equals(openId)&&openId.equals(creatorUser)){
            siteLocationService.updatePlace(siteLocation);
            return ResultVOUtil.success();
        }else{
            return ResultVOUtil.error(1,"更新失败");
        }
    }


    /**
     * 展示所有的地点  后期筛选出要在用户端显示的非分类的地点
     * @Param type 传入地点的类型
     * @return
     */
    @ApiOperation(value = "根据类型id查询地点列表",notes = "根据类型id查询地点列表")
    @GetMapping("/getSites")
    public ResultVO showSites(@ApiParam("地点类型id") String typeId){
        List<SiteLocation> sites = siteLocationService.getPlacesList(typeId);
        return ResultVOUtil.success(sites);
    }


    /**
     * 获取所有的地点类型
     * @return
     */
    @ApiOperation(value = "展示所有的地点类型",notes = "展示所有的地点类型")
    @GetMapping("/getTypes")
    public ResultVO<List<SchoolSiteType>> getSiteType(){
        List<SchoolSiteType> siteTypes = siteLocationService.getTypeList();
        return ResultVOUtil.success(siteTypes);
    }

    /**
     * 获取所有的地点
     */
    @ApiOperation(value = "获取所有的地点",notes = "获取到所有的地点")
    @RequestMapping(value = "/getAllSites",method = RequestMethod.GET)
    public ResultVO<List<SiteLocation>> getSites(){
        List<SiteLocation> place = siteLocationService.getPlace();
        return ResultUtils.success(place);

    }

    /**
     * 获取地点的详情
     * @param siteId
     * @return
     */
    @ApiOperation(value = "获取地点的详情",notes = "获取地点的详情")
    @RequestMapping(value = "/getSiteDetail",method = RequestMethod.GET)
    public ResultVO getSiteDetail(@ApiParam("地点的id") String siteId){
        SiteLocation placeById = siteLocationService.getPlaceById(siteId);
        return ResultUtils.success(placeById);
    }

}
